package model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.ibm.staf.STAFException;
import com.ibm.staf.STAFHandle;
import com.ibm.staf.STAFMarshallingContext;
import com.ibm.staf.STAFResult;


public class STAXJobLister {
	private static STAFHandle handle;

	public static void main(String[]args){
		STAXJobLister l = new STAXJobLister();
		System.out.println(l.queryJobID("simple"));
	}


	public static String queryJobID(String jobName){
        try
        {
            handle = new STAFHandle("STAXJobLister");
            STAFResult result = handle.submit2("local", "stax", "list jobs");
//            System.out.println(result.rc);
            if (STAFMarshallingContext.isMarshalledData(result.result))
            {
                STAFMarshallingContext mc = STAFMarshallingContext.unmarshall(
                    result.result);
                List entryList = (List)mc.getRootObject();

                // Check if an entry named "bin" is in the directory list

                Iterator iter = entryList.iterator();

                while (iter.hasNext())
                {
                    HashMap entry = (HashMap)iter.next();
                    if(entry.get("jobName").toString().equals(jobName))
                    	return entry.get("jobID").toString();
                }
            }
            return null;

        }
        catch (STAFException e)
        {
            e.printStackTrace();
            return null;
        }
	}


//	public int getJobID(String jobName){
//
//	}



}
